package com.ys.controller.api.finance;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.config.MenuId;
import com.ys.entity.base.TbSite;
import com.ys.entity.business.TpProductBasisComponent;
import com.ys.entity.sys.YsCol;
import com.ys.entity.sys.YsDict;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.business.YsbExportLogService;
import com.ys.service.finance.TcwAccountCountForCompanyService;
import com.ys.service.sys.UploadService;
import com.ys.util.CheckUtil;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.ys.entity.finance.TcwAccountCountForCompany;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.LayuiTableVoJA;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/inter/tcwAccountCountForCompany")
public class ApiInterTcwAccountCountForCompanyController {

	private static final Logger logger = LoggerFactory.getLogger(ApiInterTcwCapitalClaimController.class);
	
	@Autowired
	private CommonApiService<TcwAccountCountForCompany> commonApiService;

	@Autowired
	private TcwAccountCountForCompanyService tcwAccountCountForCompanyService;

	@Autowired
	private CheckUtil checkUtil;

	@RequiresPermissions("tcwAccountCountForCompany:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute TcwAccountCountForCompany cond
			){
		
		cond.addLeftJoin("THR_OperateCompanyApply B on B.VGsBm = A.VGsBm")
				.addField("B.OperationsAttribute AS operationsAttribute")//运营属性(1公司账户、2运营账户、3过渡账户、4直营账户)
			.addLeftJoin("TS_UserInfo U ON U.UserId = B.Cuser")
				.addLeftJoin("TS_OrganizationInfo OI ON OI.Orgid = U.DeptId")
				.addField("OI.OrgName AS cDeptName")//部门

			//.addField("B.CUser cUser, U.UserName cUserName, U.DeptName cDeptName")
				.addField("B.CUser cUser, U.UserName cUserName")
			.setOrderby("A.VGsBm asc");
		cond = commonApiService.dataAuth(MenuId.TCW_AccountCountForCompany, cond);
		return commonApiService.searchLay(cond);
	}



	/**
	 * 修改负载额度
	 */
	@RequiresPermissions("tcwAccountCountForCompany:updateFzed")
	@RequestMapping("/updateFzed")
	@ResponseBody
	public R updateFzed(HttpServletRequest request, @RequestParam Map<String, Object> params){
		try {
			if(checkUtil.isNullByObject(params.get("vgsbm"))){
				return R.error("运营公司编码为空");
			}
			if(checkUtil.isNullByObject(params.get("fFzEd"))){
				return R.error("负载额度为空");
			}
			if(!checkUtil.isDecimal(params.get("fFzEd").toString(),"2")){
				return R.error("负载额度应是数字");
			}
			String vgsbm = params.get("vgsbm").toString();
			BigDecimal fFzEd = new BigDecimal(params.get("fFzEd").toString());
			//修改负载额度
			tcwAccountCountForCompanyService.updateFzed(vgsbm, fFzEd);
			return R.ok();
		} catch (Exception e) {
			logger.error("新增出错：",e.getMessage(),e);
			return R.error(e.getCause().getMessage());
		}
	}

	/**
	 * 导出
	 */
	@RequiresPermissions("tcwAccountCountForCompany:export")
	@RequestMapping("/export")
	@ResponseBody
	public R export(@ModelAttribute TcwAccountCountForCompany cond) throws Exception{

		cond.addLeftJoin("THR_OperateCompanyApply B on B.VGsBm = A.VGsBm")
				.addField("B.OperationsAttribute AS operationsAttribute")//运营属性(1公司账户、2运营账户、3过渡账户、4直营账户)
				.addLeftJoin("TS_UserInfo U ON U.UserId = B.Cuser")
				.addLeftJoin("TS_OrganizationInfo OI ON OI.Orgid = U.DeptId")
				.addField("OI.OrgName AS cDeptName")//部门
				.addField("B.CUser cUser, U.UserName cUserName")
				.setOrderby("A.VGsBm asc");

		//查询数据字典信息
		List<YsDict> ysDictList = new YsDict().queryList();
		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.queryMap();

		List<YsCol> listField = new YsCol()
				.setMenuId(64L)
				.setFrontExport(YsColEnum.FrontShow.SHOW.value)
				.setOrderby("A.SortNo asc")
				.queryList();


		ExportExcelUtils<TcwAccountCountForCompany> util = new ExportExcelUtils<TcwAccountCountForCompany>(new TcwAccountCountForCompany());

		String toFilePath = util.exportExcel(ysDictList,listData, listField, "公司实时资金表");
		UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
		return R.ok(dto.getUrl());
	}
}
